package com.humanres.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciko.code.adapter.WorkFlowAdapter;
import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.RecodeListEntity;
import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AgreeTripFlowReq;
import com.ciko.code.http.Req.BackTripFlowReq;
import com.ciko.code.http.Req.FinishTripFlowReq;
import com.ciko.code.http.Req.GetTripFlowNextPerson;
import com.ciko.code.http.Req.GetTripFlowsBySelfEty;
import com.ciko.code.type.ResLeaveEnum;
import com.ciko.code.utils.DateUtil;
import com.ciko.code.utils.Util;
import com.ciko.code.views.PullListView;
import com.mywork.R;
import com.mywork.WorkUIKit;
import com.ui.dialog.ResDialog;
import com.ui.dialog.ResDialogListener;

import java.util.List;


public class ResOutDetailActivity
        extends
        BaseActicity
{
	
	private GetTripFlowsBySelfEty  getTripFlowsBySelfEty;
	private TextView			   tvNamePerson;
	private TextView			   tvStateOut;
	private TextView			   tvPlaceOut;
	private TextView			   tvCreateTimeOut;
	private TextView			   tvStartTimeOut;
	private TextView			   tvEndTimeOut;
	private TextView			   tvDayCountOut;
	private TextView			   tvResonOut;
	private Button				   btnRejectOut;
	private Button				   btnAcceptOut;
	private Button				   btnFinish;
	private LinearLayout		   llAcceptOrReject;
	private LinearLayout		   llFinish;
	private TextView			   tvaddLeave;
								   
	private PullListView		   lv_workFlow;
	private WorkFlowAdapter		   workFlowOutAdapter;
	private List<RecodeListEntity> recodeListEntityList;
	private ResLeaveEnum		   fromtype;
	private User				   nextUser;
	private String				   content;
	private ResDialog			   resDialog;
	private LinearLayout		   ll;
								   
	@Override
	protected void initTopBar() {
		
		getTripFlowsBySelfEty = (GetTripFlowsBySelfEty) getIntent().getSerializableExtra("GetTripFlowsBySelfEty");
		fromtype = (ResLeaveEnum) getIntent().getSerializableExtra("fromType");
		recodeListEntityList = getTripFlowsBySelfEty.getWorkFlow().getRecodeList();
		
		setTitle("查看出差详情");
		enableNavLeftImg();
	}
	
	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_res_item_out_detail);
	}
	
	@Override
	protected void initView() {
		ll = (LinearLayout) findViewById(R.id.ll_out);
		ll.setVisibility(View.GONE);
		btnAcceptOut = (Button) findViewById(R.id.btnAcceptOut);
		btnRejectOut = (Button) findViewById(R.id.btnRejectOut);
		tvStateOut = (TextView) findViewById(R.id.tvStateOut);
		tvDayCountOut = (TextView) findViewById(R.id.tvDayCountOut);
		tvEndTimeOut = (TextView) findViewById(R.id.tvEndTimeOut);
		tvStartTimeOut = (TextView) findViewById(R.id.tvStartTimeOut);
		tvCreateTimeOut = (TextView) findViewById(R.id.tvCreateTimeOut);
		tvPlaceOut = (TextView) findViewById(R.id.tvPlaceOut);
		tvResonOut = (TextView) findViewById(R.id.tvResonOut);
		tvNamePerson = (TextView) findViewById(R.id.tvNamePerson);
		tvaddLeave = (TextView) findViewById(R.id.tv_addLeave);
		
		llAcceptOrReject = (LinearLayout) findViewById(R.id.ll_acceptOrreject);
		llFinish = (LinearLayout) findViewById(R.id.ll_finish);
		btnRejectOut = (Button) findViewById(R.id.btnRejectOut);
		btnAcceptOut = (Button) findViewById(R.id.btnAcceptOut);
		btnFinish = (Button) findViewById(R.id.btnfinish);
		progressDialog2.show();
		if (null != fromtype && fromtype == ResLeaveEnum.Receive)
		{
			//            if ("流程审批".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) &&
			//                    WorkUIKit.getUser().getId() == getTripFlowsBySelfEty.getNextUserId()) {
			//                llAcceptOrReject.setVisibility(View.VISIBLE);
			//            } else {
			//                llAcceptOrReject.setVisibility(View.GONE);
			//                llFinish.setVisibility(View.VISIBLE);
			//            }
//			if (("归档".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || "流程结束".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) && nextUser == null) || "失败".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || "成功".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || getTripFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId())
			if ("归档".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || "失败".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批不通过".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || getTripFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId())

			{
				llAcceptOrReject.setVisibility(View.GONE);
				llFinish.setVisibility(View.GONE);

			}
			else
			{
				llAcceptOrReject.setVisibility(View.VISIBLE);
				llFinish.setVisibility(View.GONE);
			}

			if (("流程发起".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) ||"成功".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批中".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName())) && WorkUIKit.getUser().getUserPositionMark() == 12)
			{
				btnRejectOut.setVisibility(View.GONE);
				btnAcceptOut.setText("归档");
			}
		}
		else
		{
			llAcceptOrReject.setVisibility(View.GONE);
			llFinish.setVisibility(View.GONE);
		}
		
		lv_workFlow = (PullListView) findViewById(R.id.lv_workFlow);
	}
	
	@Override
	protected void initAdapter() {
		String name = getTripFlowsBySelfEty.getWorkFlow().getNodeName();
		boolean isLast = name.equals("归档")?true:false;
		workFlowOutAdapter = new WorkFlowAdapter(this, recodeListEntityList, R.layout.item_list_res_work_flow, getTripFlowsBySelfEty.getWorkFlow(), isLast);
		lv_workFlow.setAdapter(workFlowOutAdapter);
	}
	
	@Override
	protected void initDates() {
		tvNamePerson.setText(getTripFlowsBySelfEty.getUserName());
		tvPlaceOut.setText(getTripFlowsBySelfEty.getTripAndress());
		tvCreateTimeOut.setText(DateUtil.longToDate(getTripFlowsBySelfEty.getWorkFlow().getCreateTime(), 4));
		tvaddLeave.setText(getTripFlowsBySelfEty.getTripTime() < getTripFlowsBySelfEty.getWorkFlow().getCreateTime() ? "是" : "否");
//		tvStartTimeOut.setText(DateUtil.longToDate(getTripFlowsBySelfEty.getTripTime(), 4));
//		tvEndTimeOut.setText(DateUtil.longToDate(getTripFlowsBySelfEty.getReturnTime(), 4));
		String startT = DateUtil.longToDate(getTripFlowsBySelfEty.getTripTime(), 4);
		String endT = DateUtil.longToDate(getTripFlowsBySelfEty.getReturnTime(), 4);
		tvStartTimeOut.setText(AMOrPM(startT));
		tvEndTimeOut.setText(AMOrPM(endT));
//		tvDayCountOut.setText(Util.getDayAndHour((int) getTripFlowsBySelfEty.getTripDays()));
		tvDayCountOut.setText( getTripFlowsBySelfEty.getTripDays()+"天");
		tvResonOut.setText(getTripFlowsBySelfEty.getTripReason());
		tvStateOut.setText(getTripFlowsBySelfEty.getWorkFlow().getNodeName());
		
		doRequestHttp(new GetTripFlowNextPerson(getTripFlowsBySelfEty.getId()));
	}
	
	@Override
	protected void initListener() {
		btnAcceptOut.setOnClickListener(this);
		btnRejectOut.setOnClickListener(this);
		btnFinish.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		if (v == btnAcceptOut)
		{
			if (null != nextUser && getTripFlowsBySelfEty.getUserId() != getTripFlowsBySelfEty.getNextUserId())
			{
				
				resDialog = new ResDialog(this, new ResDialogListener()
				{
					@Override
					public void clickOk() {
						progressDialog.show();
						doRequestHttp(new AgreeTripFlowReq(getTripFlowsBySelfEty.getId(), nextUser.getId(), nextUser.getStaffName(), resDialog.getEidtText()));
					}
				});
				resDialog.show();
				
			}
			else
			{
//				resDialog = new ResDialog(this, new ResDialogListener()
//				{
//					@Override
//					public void clickOk() {
//						progressDialog.show();
//						doRequestHttp(new FinishTripFlowReq(getTripFlowsBySelfEty.getId(), resDialog.getEidtText()));
//					}
//				});
//				resDialog.show();
				progressDialog.show();
				doRequestHttp(new FinishTripFlowReq(getTripFlowsBySelfEty.getId(), ""));
			}
		}
		else if (v == btnRejectOut)
		{
			resDialog = new ResDialog(this, new ResDialogListener()
			{
				@Override
				public void clickOk() {
					progressDialog.show();
					doRequestHttp(new BackTripFlowReq(getTripFlowsBySelfEty.getId(), resDialog.getEidtText()));
				}
			});
			resDialog.show();
		}
		else if (v == btnFinish)
		{
			doRequestHttp(new FinishTripFlowReq(getTripFlowsBySelfEty.getId(), ""));
		}
	}
	
	@Override
	public void onResponseSuccess(BaseResponse<?> response, String tag) {
		super.onResponseSuccess(response, tag);
		switch (tag) {
			case "GetTripFlowNextPerson":
				nextUser = (User) response.getReturnObject();
				progressDialog2.dismiss();
				ll.setVisibility(View.VISIBLE);
				if (nextUser.getUserPositionMark() == 9999) {
					Util.showToast(this, R.string.params_less_tip);
				}
				break;
			case "AgreeTripFlowReq":
				progressDialog.dismiss();
				showToast("同意审批成功");
				Intent intent = new Intent();
				setResult(1002, intent);
				finish();
				break;
			case "BackTripFlowReq":
				progressDialog.dismiss();
				showToast("拒绝审批成功");
				setResult(1002);
				finish();
				break;
			case "FinishTripFlowReq":
				showToast(nextUser == null?"归档成功":"同意审批成功");
				progressDialog.dismiss();
				Intent intent1 = new Intent();
				setResult(1002, intent1);
				finish();
				break;
		}
	}
	
	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		switch (tag) {
			case "GetTripFlowNextPerson":
				progressDialog2.dismiss();
				showToast("加载失败，请稍后再试");
				finish();
				break;
			default:
				progressDialog.dismiss();
				showToast("审批失败，请稍后再试");
				break;
		}
	}

	/**
	 * 将时间转换成上下午
     */
	private String AMOrPM(String time) {
		String date = time.split(" ")[0];
		int hour = Integer.parseInt(time.split(" ")[1].split(":")[0]);
		if (hour >= 12)
			return date + " 下午";
		else
			return date + " 上午";
	}

}
